using System;
using System.Windows.Data;
namespace System.Windows.Controls
{
	[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(ContentPresenter))]
	public class HeaderedItemsControl : ItemsControl
	{
		public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(HeaderedItemsControl), new PropertyMetadata(new PropertyChangedCallback(HeaderedItemsControl.OnHeaderPropertyChanged)));
		public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(HeaderedItemsControl), new PropertyMetadata(new PropertyChangedCallback(HeaderedItemsControl.OnHeaderTemplatePropertyChanged)));
		public static readonly DependencyProperty ItemContainerStyleProperty = DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(HeaderedItemsControl), new PropertyMetadata(null, new PropertyChangedCallback(HeaderedItemsControl.OnItemContainerStylePropertyChanged)));
		internal bool HeaderIsItem
		{
			get;
			set;
		}
		public object Header
		{
			get
			{
				return base.GetValue(HeaderedItemsControl.HeaderProperty);
			}
			set
			{
				base.SetValue(HeaderedItemsControl.HeaderProperty, value);
			}
		}
		public DataTemplate HeaderTemplate
		{
			get
			{
				return base.GetValue(HeaderedItemsControl.HeaderTemplateProperty) as DataTemplate;
			}
			set
			{
				base.SetValue(HeaderedItemsControl.HeaderTemplateProperty, value);
			}
		}
		public Style ItemContainerStyle
		{
			get
			{
				return base.GetValue(HeaderedItemsControl.ItemContainerStyleProperty) as Style;
			}
			set
			{
				base.SetValue(HeaderedItemsControl.ItemContainerStyleProperty, value);
			}
		}
		internal ItemsControlHelper ItemsControlHelper
		{
			get;
			private set;
		}
		private static void OnHeaderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			HeaderedItemsControl headeredItemsControl = d as HeaderedItemsControl;
			headeredItemsControl.OnHeaderChanged(e.get_OldValue(), e.get_NewValue());
		}
		private static void OnHeaderTemplatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			HeaderedItemsControl headeredItemsControl = d as HeaderedItemsControl;
			DataTemplate oldHeaderTemplate = e.get_OldValue() as DataTemplate;
			DataTemplate newHeaderTemplate = e.get_NewValue() as DataTemplate;
			headeredItemsControl.OnHeaderTemplateChanged(oldHeaderTemplate, newHeaderTemplate);
		}
		private static void OnItemContainerStylePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			HeaderedItemsControl headeredItemsControl = d as HeaderedItemsControl;
			Style itemContainerStyle = e.get_NewValue() as Style;
			headeredItemsControl.ItemsControlHelper.UpdateItemContainerStyle(itemContainerStyle);
		}
		public HeaderedItemsControl()
		{
			base.set_DefaultStyleKey(typeof(HeaderedItemsControl));
			this.ItemsControlHelper = new ItemsControlHelper(this);
		}
		protected virtual void OnHeaderChanged(object oldHeader, object newHeader)
		{
		}
		protected virtual void OnHeaderTemplateChanged(DataTemplate oldHeaderTemplate, DataTemplate newHeaderTemplate)
		{
		}
		public override void OnApplyTemplate()
		{
			this.ItemsControlHelper.OnApplyTemplate();
			base.OnApplyTemplate();
		}
		protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
		{
			ItemsControlHelper.PrepareContainerForItemOverride(element, this.ItemContainerStyle);
			HeaderedItemsControl.PreparePrepareHeaderedItemsControlContainerForItemOverride(element, item, this, this.ItemContainerStyle);
			base.PrepareContainerForItemOverride(element, item);
		}
		internal static void PreparePrepareHeaderedItemsControlContainerForItemOverride(DependencyObject element, object item, ItemsControl parent, Style parentItemContainerStyle)
		{
			HeaderedItemsControl headeredItemsControl = element as HeaderedItemsControl;
			if (headeredItemsControl != null)
			{
				HeaderedItemsControl.PrepareHeaderedItemsControlContainer(headeredItemsControl, item, parent, parentItemContainerStyle);
			}
		}
		private static void PrepareHeaderedItemsControlContainer(HeaderedItemsControl control, object item, ItemsControl parentItemsControl, Style parentItemContainerStyle)
		{
			if (control != item)
			{
				DataTemplate itemTemplate = parentItemsControl.get_ItemTemplate();
				if (itemTemplate != null)
				{
					control.SetValue(ItemsControl.ItemTemplateProperty, itemTemplate);
				}
				if (parentItemContainerStyle != null && HeaderedItemsControl.HasDefaultValue(control, HeaderedItemsControl.ItemContainerStyleProperty))
				{
					control.SetValue(HeaderedItemsControl.ItemContainerStyleProperty, parentItemContainerStyle);
				}
				if (control.HeaderIsItem || HeaderedItemsControl.HasDefaultValue(control, HeaderedItemsControl.HeaderProperty))
				{
					control.Header = item;
					control.HeaderIsItem = true;
				}
				if (itemTemplate != null)
				{
					control.SetValue(HeaderedItemsControl.HeaderTemplateProperty, itemTemplate);
				}
				if (parentItemContainerStyle != null && control.get_Style() == null)
				{
					control.SetValue(FrameworkElement.StyleProperty, parentItemContainerStyle);
				}
				HierarchicalDataTemplate hierarchicalDataTemplate = itemTemplate as HierarchicalDataTemplate;
				if (hierarchicalDataTemplate != null)
				{
					if (hierarchicalDataTemplate.ItemsSource != null && HeaderedItemsControl.HasDefaultValue(control, ItemsControl.ItemsSourceProperty))
					{
						DependencyProperty arg_13A_1 = ItemsControl.ItemsSourceProperty;
						Binding binding = new Binding();
						binding.set_Converter(hierarchicalDataTemplate.ItemsSource.get_Converter());
						binding.set_ConverterCulture(hierarchicalDataTemplate.ItemsSource.get_ConverterCulture());
						binding.set_ConverterParameter(hierarchicalDataTemplate.ItemsSource.get_ConverterParameter());
						binding.set_Mode(hierarchicalDataTemplate.ItemsSource.get_Mode());
						binding.set_NotifyOnValidationError(hierarchicalDataTemplate.ItemsSource.get_NotifyOnValidationError());
						binding.set_Path(hierarchicalDataTemplate.ItemsSource.get_Path());
						binding.set_Source(control.Header);
						binding.set_ValidatesOnExceptions(hierarchicalDataTemplate.ItemsSource.get_ValidatesOnExceptions());
						control.SetBinding(arg_13A_1, binding);
					}
					if (hierarchicalDataTemplate.IsItemTemplateSet && control.get_ItemTemplate() == itemTemplate)
					{
						control.ClearValue(ItemsControl.ItemTemplateProperty);
						if (hierarchicalDataTemplate.ItemTemplate != null)
						{
							control.set_ItemTemplate(hierarchicalDataTemplate.ItemTemplate);
						}
					}
					if (hierarchicalDataTemplate.IsItemContainerStyleSet && control.ItemContainerStyle == parentItemContainerStyle)
					{
						control.ClearValue(HeaderedItemsControl.ItemContainerStyleProperty);
						if (hierarchicalDataTemplate.ItemContainerStyle != null)
						{
							control.ItemContainerStyle = hierarchicalDataTemplate.ItemContainerStyle;
						}
					}
				}
			}
		}
		private static bool HasDefaultValue(Control control, DependencyProperty property)
		{
			return control.ReadLocalValue(property) == DependencyProperty.UnsetValue;
		}
	}
}
